package com.yc.votesys.controller;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.votesys.bean.UserInfo;
import com.yc.votesys.biz.IUserInfoService;
import com.yc.votesys.biz.impl.UserInfoServiceImpl;
import com.yc.votesys.util.RequestParamUtil;

@WebServlet("/user/*")
public class UserInfoController extends BaseController{
	private static final long serialVersionUID = -5250826824789534111L;
	
	
	public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
		UserInfo uf = RequestParamUtil.getParames(UserInfo.class, request);
		IUserInfoService userInfoService = new UserInfoServiceImpl();
		UserInfo user = userInfoService.login(uf);
		if(user == null) {
			this.send(response, 500, "登录失败");
			return;
		}
		request.getSession().setAttribute("currentLoginUser", user);
		this.send(response, 200, "登录成功");
	}
	
	public void reg(HttpServletRequest request, HttpServletResponse response) throws IOException {
		UserInfo uf = RequestParamUtil.getParames(UserInfo.class, request);
		IUserInfoService userInfoService = new UserInfoServiceImpl();
		int result = userInfoService.reg(uf);
		if(result > 0) {
			this.send(response, 200, "注册成功");
			return;
		}
		this.send(response, 500, "注册失败");
	}
	
	public void check(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Object obj = request.getSession().getAttribute("currentLoginUser");
		if(obj == null) {
			this.send(response, 500, "失败");
			return;
		}
		this.send(response, 200, obj);
		return;
	}
}
